<template>
  <div class="login-bgc">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="头像" />
      </div>
      <el-form label-width="0px" class="login_form" :model="form" :rules="rules" ref="loginForm">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input placeholder="用户名" prefix-icon="iconfont icon-user" v-model="form.username"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
            show-password
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <!--button-->
        <el-form-item class="btns">
          <el-button type="primary" @click="submitLogin('loginForm')">确定</el-button>
          <el-button type="info" @click="resetLoginFields('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
     submitLogin(formName) {
      this.$refs[formName].validate( async valid => {
        //里面的回调返回的是我们表单验证之后的结果
        if (!valid) return
        else {
          //如果是配置了后台接口，通过数据库验证登陆结果
          const {data:res} = await this.$http.post('login',this.form);
          console.log(res)
          if(res.meta.status != 200){ return this.$message.error('登陆失败，请检查用户名或密码')}
          this.$message.success('登录成功')
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home') //编程式导航
        }
      })
    },
    resetLoginFields(formName) {
      console.log(this)
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
div.login-bgc {
  height: 100%;
  background-color: #2b4b6b;

  div.login_box {
    width: 450px;
    height: 350px;
    background-color: white;
    border-radius: 5px;
    border: 1px solid #2b4b6b;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%); /*这个50%是这个盒子的高宽*/

    div.avatar_box {
      height: 130px;
      width: 130px;
      background-color: #fff;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      box-shadow: 0 0 10px #eee;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .login_form {
      position: absolute;
      bottom: 30px;
      width: 100%;
      padding: 0 10px;
      box-sizing: border-box;

      .btns {
        display: flex;
        float: right;
      }
    }
  }
}
</style>
